package com.tsx.demo;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.properties.UnitValue;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;

public class AddImageSetPosition {
    public static void main(String[] args) {
        // Creating a PdfWriter object
        String dest = Paths.get(Constants.dest, "addImageSetPosition.pdf").toString();
        try {
            PdfWriter writer = new PdfWriter(dest);
            PdfDocument pdfDocument = new PdfDocument(writer);
            float height = pdfDocument.getDefaultPageSize().getHeight();
            System.out.println("height = " + height);
            Document document = new Document(pdfDocument);
            URL resource = AddImageSetPosition.class.getClassLoader()
                    .getResource("avatar.jpg");
            assert resource != null;
            ImageData imageData = ImageDataFactory.create(resource);
            Image image = new Image(imageData);
            image.setWidth(UnitValue.createPointValue(100));
            image.setFixedPosition(50, 600);
            image.setRotationAngle(45);     // 旋转
            document.add(image);
            document.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
